/*动作序列由以下部分组成：
selfId:发起方的ID
selfAction:发起方动作
targetAction:承受方动作
selfData:我方改变的数据序列 ,eg.{hp:-1}.
targetData:对方改变的数据序列
对应平手，ID小的为发起方，其余情况胜者为发起方
一轮可能有1个或者多个动作
*/
var ACTION_EXECUTE_TYPE = {

}


function BattleRecord(){
    this.round = 0;
    //play card result
    this.actions = new Array(BattleRecord.MAX_BATTLE_ROUND);
    //cards request play and play mode
    this.requests = new Array(BattleRecord.MAX_BATTLE_ROUND);
    //cards really played
    this.playCards = new Array(BattleRecord.MAX_BATTLE_ROUND);
    //draw cards result
    this.draws = new Array(BattleRecord.MAX_BATTLE_ROUND);
}

BattleRecord.MAX_BATTLE_ROUND = 30;

/*@param {Number} playerId
* @param {Number} current round
* @param {Array} index list of cards, eg [1,2,3,4] means cards with index 1,2,3,4 in CardContainer
* @param {Bool} if guard mode
* */
BattleRecord.prototype.pushRequest = function(playerId,round,cards_index,isguard){
    if(round != this.round){
        return false;
    }
    var requests = this.requests[round] || {};
    requests[playerId] = [cards_index,isguard];
    this.requests[round] = requests;
    return true;
};

BattleRecord.prototype.getRequest = function(playerId,round){
    if(this.requests[round]){
        return this.requests[round][playerId];
    }
    return null;
}

/*
* @param {Number} playerId
* @param {Number} current round
* @param {Array} cards struct list
* */
BattleRecord.prototype.pushDraw = function(playerId,round,cards){
    if(round != this.round){
        return false;
    }
    var draw = this.draws[round] || {};
    draw[playerId] = cards;
    this.draws[round] = draw;
    return true;
}

/*
 * @param {Number} playerId
 * @param {Number} current round
 * @param {Array} cards struct list
 * */
BattleRecord.prototype.pushPlay = function(playerId,round,cards){
    if(round != this.round){
        return false;
    }
    var playCards = this.playCards[round] || {};
    playCards[playerId] = cards;
    this.playCards[round] = playCards;
    return true;
}


/*
* @param {Number} current round
* @param {Number} playerId of initiator
* @param {ACTION_TYPE} action of initiator
* @param {Number} playerId of acceptor
* @param {ACTION_TYPE} action of acceptor
* @param {Obj} affected initiator data, eg {"hp":-10} means lost 10 Hp;
* @param {Obj} affected targetDate data
* */
BattleRecord.prototype.pushAction = function(round,selfId,selfAction,targetId,targetAction,selfDate,targetDate){
    if(round != this.round){
        return false;
    }
    var actions = this.actions[round] || [];
    actions.push([round,selfId,selfAction,targetId,targetAction,selfDate,targetDate]);
    this.actions[round] = actions;
    return true;
};

BattleRecord.prototype.newRound = function(){
    if(this.round + 1 > BattleRecord.MAX_BATTLE_ROUND){
        return false;
    }
    ++this.round;
    return true;
};

BattleRecord.prototype.encodeRound = function(round){
    if(this.round < round){
        return null;
    }
    return JSON.stringify({"draw":this.draws[round],"request":this.requests[round],"action":this.actions[round]});
};

BattleRecord.prototype.decodeRound = function(round,data){
    var t = JSON.parse(data);
    if(t.hasOwnProperty("draw")){
        this.draws[round] = t["draw"];
    }
    if(t.hasOwnProperty("request")){
        this.requests[round] = t["request"];
    }
    if(t.hasOwnProperty("action")){
        this.action[round] = t["action"];
    }
}

module.exports = BattleRecord;



